package common

import (
	"fmt"

	"github.com/quic-go/quic-go"
)

// NewErrorMessage 创建错误消息
func NewErrorMessage(tunnelID, requestID string, errorCode int, errorMsg string) *TunnelMessage {
	errorBody := fmt.Sprintf(`{"error_code":%d,"error_message":"%s"}`, errorCode, errorMsg)
	return &TunnelMessage{
		Version:      ProtocolVersion,
		MessageType:  MessageTypeError,
		ProtocolType: ProtocolHTTP,
		TunnelID:     tunnelID,
		RequestID:    requestID,
		ConnectionID: "",
		Body:         []byte(errorBody),
		BodyLength:   uint64(len(errorBody)),
	}
}

// ReadMessage 从stream读取消息
func ReadMessage(stream quic.Stream) (*TunnelMessage, error) {
	// TODO: 实现消息读取逻辑
	// 这个函数应该在quic_protocol.go中定义，这里只是占位
	return nil, fmt.Errorf("ReadMessage not implemented in common package")
}

// ChunkMessage 分块消息（用于流式传输）
type ChunkMessage struct {
	TunnelID  string
	RequestID string
	ChunkID   uint64
	IsLast    bool
	Data      []byte
}

// NewChunkMessage 创建分块消息
func NewChunkMessage(tunnelID, requestID string, chunkID uint64, isLast bool, data []byte) *ChunkMessage {
	return &ChunkMessage{
		TunnelID:  tunnelID,
		RequestID: requestID,
		ChunkID:   chunkID,
		IsLast:    isLast,
		Data:      data,
	}
}

// Write 写入消息到stream（占位符）
func (m *TunnelMessage) Write(stream quic.Stream) error {
	// TODO: 实现消息写入逻辑
	// 这个方法应该在quic_protocol.go中定义
	return fmt.Errorf("Write not implemented in common package")
}
